define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Employeemanager_rights_add, GoDialog, $stateParams, GoPage) {

		$scope.input = new Object();
		$scope.input.name = '';
		$scope.partdetail = new Array();
		if($stateParams.id) {
			$scope.title="修改角色";
			$scope.input.id = $stateParams.id;
		}else{
			$scope.title="添加角色";
		}

		$scope.init_show = function() {
			Employeemanager_rights_add.get_rights(function(data) {
				//console.log(data)
				$scope.rights = new Object();
				for(var key in data) {
					//					console.log(data[key])
					var obj = data[key];
					if(obj.right_type) {
						if($scope.rights[obj.right_type]) {} else {
							$scope.rights[obj.right_type] = new Object();
							$scope.rights[obj.right_type]['type_name'] = obj.right_type;
							$scope.rights[obj.right_type]['rights'] = new Object();
							$scope.rights[obj.right_type]['is_all'] = false;
						}
						if($scope.rights[obj.right_type]['rights'][obj.right_id]) {} else {
							$scope.rights[obj.right_type]['rights'][obj.right_id] = new Object();
							$scope.rights[obj.right_type]['rights'][obj.right_id]['right_name'] = obj.right_name;
							$scope.rights[obj.right_type]['rights'][obj.right_id]['right_id'] = obj.right_id;
							$scope.rights[obj.right_type]['rights'][obj.right_id]['id_checked'] = false;
							if($scope.partdetail&&$scope.partdetail.length>0) {					
								for(var i = 0; i < $scope.partdetail.length; i++) {
									if($scope.partdetail[i].right_id == $scope.rights[obj.right_type]['rights'][obj.right_id].right_id) {
										$scope.rights[obj.right_type]['rights'][obj.right_id]['id_checked'] = true;
										break;
									} 
								}							
							}
						}
					}
				}
				for(var j in $scope.rights){
					var flag=true;
					for(var i in $scope.rights[j].rights){						
						if(!$scope.rights[j].rights[i]['id_checked']){
							flag=false;
						}
					}
					$scope.rights[j]['is_all']=flag;
				}
				
			})
		}

		$scope.init_data = function() {

			if($scope.input.id) {
				Employeemanager_rights_add.partdetail($scope.input.id, function(data) {
					//console.log(data)
					$scope.partdetail = data.result.workground;
					$scope.input.name = data.result.name;
					$scope.init_show();
				})
			} else {
				$scope.init_show();
			}

		}
		$scope.init_data();

		$scope.all = function(right_name) {
			for(var key in $scope.rights) {
				if($scope.rights[key]['type_name'] == right_name) {
					for(var o in $scope.rights[key]['rights']) {
						if($scope.rights[key]['is_all']) {
							$scope.rights[key]['rights'][o]['id_checked'] = false;
						} else {
							$scope.rights[key]['rights'][o]['id_checked'] = true;
						}
					}
					$scope.rights[key]['is_all'] = !$scope.rights[key]['is_all'];
				}
			}
		}

		//添加店员角色--保存数据
		$scope.save = function() {
			$scope.right = new Array();
			var rightIndex = 0;
			for(var key in $scope.rights) {
				for(var o in $scope.rights[key]['rights']) {
					if($scope.rights[key]['rights'][o]['id_checked']) {
						$scope.right[rightIndex] = {
							"id": $scope.rights[key]['rights'][o]['right_id']
						};
						rightIndex++;
					}
				}
			}

	/*		var rightBox = document.getElementsByClassName("rightBox");
			for(var i = 0; i < rightBox.length; i++) {
				if(rightBox[i].checked) {

					$scope.right[rightIndex] = {
						"id": rightBox[i].value
					};
					rightIndex++;
				}
			}*/
			if(!$scope.input.name) {
				GoDialog.toast("请输入角色名称！");
			} else if($scope.right.length == 0) {
				GoDialog.toast("请选择权限！");
			} else {
				//console.log($scope.right)
				Employeemanager_rights_add.get_add_eadit($scope.input.id, $scope.input.name, $scope.right, function(data) {
					if(data.success) {
						GoDialog.toast("提交数据成功！");
						$scope.init_data();
						GoPage.go_states("employeemanager");
					} else {
						GoDialog.toast(data.msg);
					}
				});
			}
		}
		//清空角色名称
		$scope.claerrolename = function() {
			$scope.input.name = "";
		}
		//改变选项
		$scope.getid_checked = function(type_name, index) {
			var flag = true;
			$scope.rights[type_name]['rights'][index]['id_checked'] = !$scope.rights[type_name]['rights'][index]['id_checked'];
			for(var o in $scope.rights[type_name]['rights']) {
				if(!$scope.rights[type_name]['rights'][o]['id_checked']) {
					flag = false;
				}
			}
			if(!flag) {
				$scope.rights[type_name]['is_all'] = false;
			} else {
				$scope.rights[type_name]['is_all'] = true;
			}
		}

	}

	controller.$inject = ['$scope', 'Employeemanager_rights_add', 'GoDialog', '$stateParams', 'GoPage'];

	return app.registerController("employeemanager_rights_addctrl", controller);
});